package com.jdk8.demo.functional.predicate;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * 断定型接口
 * 参数：boolean
 * 方法:test(T t)
 */
public class PredicateTest {
    public static void main(String[] args) {
        Predicate<String> predicate = s -> s.length() > 10;
        System.out.println(predicate.test("apple"));
        System.out.println("----------------------------");

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        conditionFilter(list, item -> item % 2 == 0);
        System.out.println("----------------------------");
        conditionFilter(list, item -> true);
        System.out.println("----------------------------");

        addTest(list,integer -> integer>5,integer -> integer % 3==0);
        System.out.println("----------------------------");
        orTest(list,integer -> integer >6,integer -> integer <5);
        System.out.println("----------------------------");

        negateTest(list,integer -> integer < 0 );
        System.out.println("----------------------------");

        System.out.println(Predicate.isEqual("test").test("abc"));

    }


    private static void conditionFilter(List<Integer> list, Predicate<Integer> predicate) {
        // list.stream().filter(predicate).forEach(System.out::println);
        for (Integer integer : list) {
            if (predicate.test(integer)) {
                System.out.println(integer);
            }
        }
    }


    private static void addTest(List<Integer> list, Predicate<Integer> predicate1, Predicate<Integer> predicate2) {
        for (Integer integer : list) {
            if (predicate1.and(predicate2).test(integer)) {
                System.out.println(integer);
            }
        }
    }

    private static void orTest(List<Integer> list,Predicate<Integer> predicate1,Predicate<Integer> predicate2){
        for (Integer integer : list) {
            if (predicate1.or(predicate2).test(integer)) {
                System.out.println(integer);
            }
        }
    }

    private static void negateTest(List<Integer> list,Predicate<Integer> predicate){
        for (Integer integer : list) {
            if (predicate.negate().test(integer)) {
                System.out.println(integer);
            }
        }
    }


}